package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.WorkreportDao;
import com.bdqn.oa.entity.Workreport;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author Zhouxw
 * @date 2022/7/21 7:55
 */
public class WorkreportDaoImpl implements WorkreportDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 新增一条工作汇报数据
     *
     * @param workreport 工作汇报
     * @return 影响行数
     */
    @Override
    public int saveWorkreport(Workreport workreport) throws SQLException {
        String sql = "insert into workreport (employee_id, date, start_time, end_time, work_name, work_content, iscore ,`status`) value " +
                "(?, ?, ?, ?, ?, ?, ?, ?)";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, workreport.getEmployeeId(), workreport.getDate(), workreport.getStartTime(), workreport.getEndTime(),
                workreport.getWorkName(), workreport.getWorkContent(), workreport.getIsCore(), workreport.getStatus());
        return result;
    }

    /**
     * 更新工作汇报数据(主要跟新状态和点评id)
     *
     * @param workreport 工作汇报
     * @return 影响行数
     */
    @Override
    public int updateWorkreport(Workreport workreport) {
        return 0;
    }

    /**
     * 根据员工id和日期更新工作汇报状态
     * 状态 1.已保存2.已提交3.已通过 4.已驳回
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param employeeId 员工id
     * @param date       日期
     * @param status     状态
     * @return 影响行数
     */
    @Override
    public int updateStatus(Integer employeeId, LocalDate date, Integer status) throws SQLException {
        String sql = "update workreport set `status` = ? where employee_id = ? and date = ?";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, status, employeeId, date);
        return result;
    }

    /**
     * 根据员工id和日期更新工作汇报点评
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param employeeId 员工id
     * @param date       日期
     * @param commentId  点评id
     * @return 影响行数
     */
    @Override
    public int updateComment(Integer employeeId, LocalDate date, Integer commentId) throws SQLException {
        String sql = "update workreport set comment_id = ? where employee_id = ? and date = ?";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, commentId, employeeId, date);
        return result;
    }

    /**
     * 通过id查询工作汇报
     *
     * @param id 工作汇报id
     * @return 工作汇报
     */
    @Override
    public Workreport getWorkreportById(Integer id) throws SQLException {
        String sql = "select * from workreport where id = ?";
        Workreport workreport = queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<Workreport>(Workreport.class), id);
        return workreport;
    }

    /**
     * 通过时间范围查询及员工id查询工作汇报
     * 查询某一天工作工作汇报需要startDate==endDate
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param startDate  起始时间
     * @param endDate    结束时间
     * @param employeeId 员工id
     * @return 工作汇报集合
     */
    @Override
    public List<Workreport> listWorkreportByDateScope(LocalDate startDate, LocalDate endDate, Integer employeeId) throws SQLException {
        String sql = "select * from workreport where employee_id = ? and date between ? and ?";
        List<Workreport> workreportList = queryRunner.query(DruidUtils.getConnection(), sql, new BeanListHandler<>(Workreport.class),
                employeeId, startDate, endDate);
        return workreportList;
    }
}
